using UnityEngine;

namespace PhysSound
{
	public class PhysSoundTempAudioPool : MonoBehaviour
	{
		public static int TempAudioPoolSize = 100;

		public static PhysSoundTempAudioPool Instance;

		private PhysSoundTempAudio[] audioSources;

		private int lastAvailable;

		public static void Create()
		{
			if (!(Instance != null))
			{
				GameObject gameObject = new GameObject("PhysSound Temp Audio Pool");
				PhysSoundTempAudioPool physSoundTempAudioPool = gameObject.AddComponent<PhysSoundTempAudioPool>();
				physSoundTempAudioPool.Initialize();
			}
		}

		public static AudioSource GetAudioSourceCopy(AudioSource template, GameObject g)
		{
			AudioSource audioSource = g.AddComponent<AudioSource>();
			if (!template)
			{
				return audioSource;
			}
			GetAudioSourceCopy(template, audioSource);
			return audioSource;
		}

		public static void GetAudioSourceCopy(AudioSource template, AudioSource target)
		{
			target.bypassEffects = template.bypassEffects;
			target.bypassListenerEffects = template.bypassListenerEffects;
			target.bypassReverbZones = template.bypassReverbZones;
			target.dopplerLevel = template.dopplerLevel;
			target.ignoreListenerPause = template.ignoreListenerPause;
			target.ignoreListenerVolume = template.ignoreListenerVolume;
			target.loop = template.loop;
			target.maxDistance = template.maxDistance;
			target.minDistance = template.minDistance;
			target.mute = template.mute;
			target.outputAudioMixerGroup = template.outputAudioMixerGroup;
			target.panStereo = template.panStereo;
			target.pitch = template.pitch;
			target.playOnAwake = template.playOnAwake;
			target.priority = template.priority;
			target.reverbZoneMix = template.reverbZoneMix;
			target.rolloffMode = template.rolloffMode;
			target.spatialBlend = template.spatialBlend;
			target.spread = template.spread;
			target.time = template.time;
			target.timeSamples = template.timeSamples;
			target.velocityUpdateMode = template.velocityUpdateMode;
			target.volume = template.volume;
		}

		public void Initialize()
		{
			Instance = this;
			audioSources = new PhysSoundTempAudio[TempAudioPoolSize];
			for (int i = 0; i < TempAudioPoolSize; i++)
			{
				GameObject gameObject = new GameObject("Temp Audio Source");
				PhysSoundTempAudio physSoundTempAudio = gameObject.AddComponent<PhysSoundTempAudio>();
				physSoundTempAudio.Initialize(this);
				audioSources[i] = physSoundTempAudio;
			}
		}

		public void PlayClip(AudioClip clip, Vector3 point, AudioSource template, float volume, float pitch)
		{
			int num = 0;
			int num2 = lastAvailable;
			PhysSoundTempAudio physSoundTempAudio;
			while (true)
			{
				if (num < TempAudioPoolSize)
				{
					physSoundTempAudio = audioSources[num2];
					if (!physSoundTempAudio.gameObject.activeInHierarchy)
					{
						break;
					}
					num2++;
					num++;
					if (num2 >= TempAudioPoolSize)
					{
						num2 = 0;
					}
					continue;
				}
				return;
			}
			physSoundTempAudio.PlayClip(clip, point, template, volume, pitch);
			lastAvailable = num2;
		}
	}
}
